//
//  SYDetailCell.swift
//  SingWeibo
//
//  Created by shiying on 15/12/25.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

class SYDetailCell: UITableViewCell {
    
    var comments: SYComments? {
        didSet {
           
            contentLabel.text = comments?.text
            
            
            /// 设置头像
            if let profile_image_url = comments?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            nameLable.text = comments?.user?.screen_name
            
            vipView.image = comments?.user!.mbrankImage
            
            timeLable.text = "08:00"
            
        }
    }
    
    var status: SYStatus? {
        didSet {
            self.comments = status?.comments

        }
    }

    func rowHeiht(comments: SYComments) -> CGFloat {
        self.comments = comments
        
        layoutIfNeeded()
    
        let MaxY = CGRectGetMaxY(contentLabel.frame)
        
        return MaxY
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(nameLable)
        contentView.addSubview(vipView)
        contentView.addSubview(timeLable)
        contentView.addSubview(contentLabel)
        contentView.addSubview(praiseButton)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.left.equalTo(contentView).offset(8)
            make.width.equalTo(35)
            make.height.equalTo(35)
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(8)
            make.top.equalTo(iconView)
            
        }
        
        vipView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLable.snp_right).offset(8)
            make.top.equalTo(nameLable)
            
        }
        
        timeLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(8)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(8)
            make.left.equalTo(contentView).offset(8)
            make.right.equalTo(contentView).offset(-8)
        }

        praiseButton.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(contentView.snp_centerY)
            make.right.equalTo(contentView.snp_right).offset(-8)
        }
    
    }
    
    // 懒加载
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        
        
        return label
    }()
    
    /// 头像
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 昵称
    lazy var nameLable: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.blackColor()
        
        return label
    }()
    
    /// vip
    lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    
    /// 时间
    lazy var timeLable: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(10)
        
        return label
    }()
    
    lazy var praiseButton: UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "timeline_icon_like"), forState: UIControlState.Normal)
        
        return button
    }()
}
